﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using RN.ERP.BLL;
using RN.ERP.DTO;
using System.Web.Services;
using Newtonsoft.Json;

namespace RN.ERP.Web.Material.Material
{
    public partial class MaterialList : BasePage
    {
        MaterialBLL bll = new MaterialBLL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                BindData();
                RadGridList.DataBind();
            }
        }

        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void RadGridList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "del":
                    int kcId = 0;
                    int.TryParse(e.CommandArgument.ToString(), out kcId);
                    DTO.OperationResult result = bll.DelteMaterial(kcId, UserInfo.CorpUserGuid);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',RefreshWin);", true);
                    break;
                case "go":
                    string id = e.CommandArgument.ToString();
                    Response.Redirect("mplist.aspx?id=" + id);
                    break;
                default:
                    break;
            }
        }

        public string FormatNum(object dMEvaluate)
        {
            if (dMEvaluate == null)
            {
                return string.Empty;
            }
            decimal m = 0;
            if (decimal.TryParse(dMEvaluate.ToString(), out m))
            {
                return m.ToString("n");
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetCount(object dMEvaluate, object iMNum)
        {
            decimal m = 0;
            int n = 0;
            if (dMEvaluate == null || iMNum == null)
            {
                return string.Empty;
            }
            if (!decimal.TryParse(dMEvaluate.ToString(), out m))
            {
                return string.Empty;
            }
            if (!int.TryParse(iMNum.ToString(), out n))
            {
                return string.Empty;
            }
            return (m * n).ToString("n");
        }

        private void BindData()
        {
            string condition = hdnCondition.Value;
            RadGridList.AllowCustomPaging = true;
            int pageCount = RadGridList.CurrentPageIndex;
            int pageSize = RadGridList.PageSize;
            int count = 0;
            List<DTO.Material> properties = new List<DTO.Material>();
            if (string.IsNullOrEmpty(condition))
            {
                properties = bll.GetMateraials(
                    pageCount,
                    pageSize,
                    out count);
            }
            else
            {

                properties = bll.GetMateraials(
                    JsonConvert.DeserializeObject<DTO.QueryMaterialCondition>(condition),
                    pageCount,
                    pageSize,
                    out count);
            }
            RadGridList.VirtualItemCount = count;
            RadGridList.DataSource = properties;
        }
    }
}